<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class UserCarModel extends AbstractModel
{
    protected $tableName = 't_user_car';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getUser($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addUser($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('用户汽车表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('uid',16)->setDefaultValue(0)->setIsNotNull()->setColumnComment('用户id');
        $table->colInt('park',8)->setDefaultValue(0)->setColumnComment('车位号');
        $table->colVarChar('image',255)->setDefaultValue(0)->setColumnComment('车辆照片');
        $table->colVarChar('name',64)->setDefaultValue(0)->setColumnComment('业主姓名');
        $table->colVarChar('tel',12)->setDefaultValue(0)->setColumnComment('业主手机号');
        $table->colVarChar('number',12)->setDefaultValue(0)->setColumnComment('业主车牌号');
        $table->colVarChar('id_card',12)->setDefaultValue(0)->setColumnComment('身份证号');
        $table->colVarChar('type',12)->setDefaultValue(0)->setColumnComment('车辆类型');
        $table->colVarChar('brand',12)->setDefaultValue(0)->setColumnComment('品牌');
        $table->colVarChar('model',12)->setDefaultValue(0)->setColumnComment('车系');
        $table->colInt('start_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('开始时间');
        $table->colInt('end_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('结束时间');
        $table->colTinyInt('type', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('汽车类型：0.小型车，1.中型车，2.大型车');
        $table->colTinyInt('status', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('状态：0.未审核，1.审核成功');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('uid_index', 'uid');
        $table->indexNormal('park_index', 'park');
        $table->indexNormal('type_index', 'type');
        $table->indexNormal('status_index', 'status');

        return $table;
    }
}